<template>
    <div class="article-edit">
        <el-card class="!border-none" shadow="never">
            <el-page-header :content="$route.meta.title" @back="$router.back()" />
        </el-card>
        <el-card class="mt-4 !border-none" shadow="never">
            <el-form
                ref="formRef"
                class="ls-form"
                :model="formData"
                label-width="85px"
                :rules="rules"
            >
                <!-- <div class="xl:flex"> -->
                    <div>
                        <el-form-item label="文章标题" prop="title">
                            <div class="w-[500px]">
                                <el-input
                                    v-model="formData.title"
                                    placeholder="请输入文章标题"
                                    type="textarea"
                                    :autosize="{ minRows: 3, maxRows: 3 }"
                                    maxlength="64"
                                    show-word-limit
                                    clearable
                                />
                            </div>
                        </el-form-item>
                        
                        <el-form-item label="文章类型" required prop="type">
                            <el-radio-group v-model="formData.type">
                                <el-radio :value="1">动态</el-radio>
                                <el-radio :value="2">文化</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="文章内容" prop="content">
                            <editor v-model="formData.content" :height="667" :width="800" :isShowTool="true"/>
                        </el-form-item>
                    </div>
            </el-form>
        </el-card>
        <footer-btns>
            <el-button type="primary" @click="handleSave">保存</el-button>
        </footer-btns>
    </div>
</template>

<script lang="ts" setup name="articleListsEdit">
import type { FormInstance } from 'element-plus'

import { add_article, info_article, edit_article } from '@/api/family'
import { useDictOptions } from '@/hooks/useDictOptions'
import { useRoute, useRouter } from 'vue-router'
import { reactive, shallowRef, onActivated } from 'vue'
import feedback from '@/utils/feedback'
const route = useRoute()
const router = useRouter()
const formData = reactive<{
    id?: string | number,
    title: string,
    content: string,
    type: any,
}>({
    title: '',
    content: '',
    type: null as any,
})

const formRef = shallowRef<FormInstance>()
const rules = reactive({
    title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
    type: [{ required: true, message: '请输入文章类型', trigger: 'blur' }],
    content: [{ required: true, message: '请输入文章内容', trigger: 'blur' }],
})

const getDetails = async () => {
    const data = await info_article({
        id: route.query.id
    })
    Object.keys(formData).forEach((key) => {
        //@ts-ignore
        formData[key] = data[key]
    })
    formData.id = route.query.id as any
}


const handleSave = async () => {
    await formRef.value?.validate()
    if (route.query.id) {
        await edit_article({...formData, family_id: route.query.family_id})
    } else {
        await add_article({...formData, family_id: route.query.family_id})
    }
    await feedback.msgSuccess('保存成功')
    router.back()
}

route.query.id && getDetails()
</script>
